package com.jivam.lottery.panel;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import com.jivam.lottery.data.DataCalculator;
import com.jivam.lottery.data.DataUtil;
import com.jivam.lottery.main.Analyse;
import com.jivam.lottery.model.Formula;
import com.jivam.lottery.model.KeyFormula;
import com.jivam.lottery.model.PeriodNum;
import com.jivam.lottery.util.Constants;
import com.jivam.lottery.util.ShowUtil;

/**
 * 抽象公式面板
 * @author Jivam
 *
 */
public class FormulaPanel extends AbstractPanel{
	
	private static final long serialVersionUID = -3825225093594114271L;
	private KeyFormula kf;
	private List<List<JLabel>> pls = new ArrayList<List<JLabel>>(Constants.MAX_FORMULA_DATA_SIZE);
	private List<JLabel> titles = new ArrayList<JLabel>(Constants.MAX_FORMULA_DATA_SIZE);
	
	public FormulaPanel(KeyFormula kf){
		super();
		this.kf = kf;
		init();
	}

	@Override
	public void init() {
		this.setLayout(new GridLayout(kf.getFormulas().size()+1,Constants.SIZE_PANEL_COLS));
		try {
			reloadPanel();
			int nextPid = DataCalculator.getNextPid();
			for(int i=0;i<kf.getFormulas().size();i++){
				if(i == 0){
					this.add(new JLabel("",SwingConstants.CENTER));
					JLabel title = null;
					for(int j = 1;j<Constants.SIZE_PANEL_COLS;j++){
						title = new JLabel((nextPid - Constants.MAX_FORMULA_DATA_SIZE + j)%100+"",SwingConstants.CENTER);
						this.add(title);
						titles.add(title);
					}
				}
				final JLabel fl = new JLabel(Constants.LABEL_FORMULA_PREFIX+(i+1),SwingConstants.CENTER);
				fl.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				fl.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						int index = Integer.parseInt(fl.getText().replaceFirst(Constants.LABEL_FORMULA_PREFIX, ""));
						Formula f = kf.getFormulas().get(index-1);
						String title = kf.getKey()+" - "+fl.getText()+"公式详情";
						String content = fl.getText()+" = "+f.getExpression()+" 准确率:"+f.getWrongRate()+"% 连续2次准确率:"+f.getDoubleWrongRate()+"%";
						JOptionPane.showMessageDialog(Analyse.getInstanse(),content,title, JOptionPane.INFORMATION_MESSAGE);
					}
				});
				this.add(fl);
				for(int j = 0 ; j < Constants.MAX_FORMULA_DATA_SIZE ; j ++){
					this.add(pls.get(j).get(i));
				}
			}
		} catch (Exception e) {
			this.add(new JLabel("FormulaPanel 初始化错误"+e.getMessage()));
			DataUtil.writeLog(e);
		}
	}
	
	@Override
	public void reloadPanel() {
		List<JLabel> lbs = null;
		JLabel lb = null;
		PeriodNum tpn = null;
		Map<String,Integer> numDict = null;
		LinkedList<int[]> temp = kf.getDatas();
		if(pls.size() == 0){//初始化的时候调用
			for(int[] pn : temp){
				lbs = new ArrayList<JLabel>();
				tpn = DataCalculator.getPeriod(pn[0]);
				numDict = DataCalculator.getDict(tpn);
				for(int i = 1 ; i < pn.length ; i++){
					lb = ShowUtil.getCalLabel(pn[i]);
					lb.setForeground((tpn != null && DataCalculator.isGuessRight(pn[i],numDict,kf.getExpressions())) ? Color.RED : Color.BLACK);
					lbs.add(lb);
				}
				pls.add(lbs);
			}
		}else{//添加后调用
			int[] pn = null;
			for(int i =0 ; i<temp.size();i++){
				pn = temp.get(i);
				lbs = pls.get(i);
				tpn = DataCalculator.getPeriod(pn[0]);
				numDict = DataCalculator.getDict(tpn);
				lb = titles.get(i);
				lb.setText((pn[0])%100+"");
				for(int j = 1 ; j < pn.length ; j++){
					lb = lbs.get(j-1);
					lb.setText(pn[j]+"");
					lb.setForeground((tpn != null && DataCalculator.isGuessRight(pn[j],numDict,kf.getExpressions())) ? Color.RED : Color.BLACK);
				}
				pls.add(lbs);
			}
		}
	}
	
	
	public void reloadKeyFormula(KeyFormula kf) {
		this.kf = kf;
	}
	
}
